package com.seeyon.apps.zhiwei.nbtest.util;

import cn.hutool.core.convert.Convert;
import com.seeyon.cap4.form.bean.*;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author zhuon
 * @desc 表单操作工具类
 * @version  2020.07.03
 */
@Slf4j
public class FormUtil {

	public static FormBean getFormBean(CAP4FormManager cap4FormManager, String code) {
		FormBean formBean = null;
		try {
			formBean = cap4FormManager.getFormByFormCode(code);
		} catch(BusinessException e) {
			log.error("获取表单发生异常,编号：" + code, e);
		}
		return formBean;
	}

	/**
	 * 根据表单的显示名称获取字段的值
	 * @param bean
	 * @param disPlay
	 * @return
	 */
	public static Object getFieldValue(FormDataBean bean, String disPlay) {
		if(bean == null) {
			return null;
		}
		FormTableBean table = bean.getFormTable();
		if(table == null) {
			return null;
		}
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		if(field == null) {
			return null;
		}
		return bean.getFieldValue(field.getName());
	}


	public static int getIntValue(FormDataBean bean, String disPlay) {
		Object value = getFieldValue(bean, disPlay);
		return Convert.toInt(value);
	}

	public static String getFieldStrValue(FormDataBean bean, String disPlay) {
		Object value = getFieldValue(bean, disPlay);
		return Convert.toStr(value);
	}


	//demo
	@Deprecated
	public static String getFieldName(FormDataBean bean, String disPlay) {
		FormTableBean table = bean.getFormTable();
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		return field.getName();
	}

	//demo
	@Deprecated
	public static String getFieldName(FormBean bean, String disPlay) {
		FormTableBean table = bean.getMasterTableBean();
		FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
		return field.getName();
	}

	//demo
	@Deprecated
	public static FormFieldBean getFieldBean(FormDataBean bean, String disPlay) {
		FormTableBean table = bean.getFormTable();
		return table.getFieldBeanByDisplay(disPlay);
	}

	//demo
	@Deprecated
	public static FormFieldBean getFieldBean(FormBean bean, String disPlay) {
		FormTableBean table = bean.getMasterTableBean();
		return table.getFieldBeanByDisplay(disPlay);
	}

	//demo
	@Deprecated
	public static FormFieldBean getFieldBean(FormTableBean bean, String disPlay) {
		return bean.getFieldBeanByDisplay(disPlay);
	}


	/**
	 * @param bean 这里的bean必须是getMasterBean() 方法获取到的bean
	 */
	public static void setCellValue(FormDataBean bean, String disPlay, Object value) {
		FormFieldBean cell = bean.getFormTable().getFieldBeanByDisplay(disPlay);
		if(cell != null) {
			bean.addFieldValue(cell.getName(), value);
		}
	}

	/**
	 * <pre>只适用于只有一个子表的表单</pre>
	 */
	public static List<FormDataSubBean> getSubBeans(FormDataMasterBean master) throws Exception {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();
		if(null != subs && subs.size() > 0) {
			for(String key : subs.keySet()) {
				return subs.get(key);
			}
		}
		return null;
	}

	//把表单list转换成map，方便取值
	private static Map<String, FormTableBean> toFormBeanMap(List<FormTableBean> tableList) {
		return tableList.stream().collect(Collectors.toMap(table -> table.getDisplay(),	Function.identity(),	(key1, key2) -> key2));
	}

	//获取子表表结构Map
	public static Map<String, FormTableBean> getFormBeanMap(CAP4FormManager cap4FormManager, FormDataMasterBean bean) {
		Long formId = bean.getFormTable().getFormId();
		List<FormTableBean> tableList = cap4FormManager.getForm(formId, false).getTableList();
		return toFormBeanMap(tableList);
	}

	//获取子表数据Map
	public static Map<String, List<FormDataSubBean>> getSubDataMap(FormDataMasterBean master) throws Exception {
		Map<String, List<FormDataSubBean>> subs = master.getSubTables();
		Map<String, List<FormDataSubBean>> dictMap = new HashMap<>();
		if(null != subs && subs.size() > 0) {
			for(String key : subs.keySet()) {
				dictMap.put(subs.get(key).get(0).getFormTable().getDisplay(), subs.get(key));
			}
		}
		return dictMap;
	}
}
